package pl.edu.pw.elka.tinyrepo.client.view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.concurrent.BlockingQueue;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import pl.edu.pw.elka.tinyrepo.client.common.COPYRequest;
import pl.edu.pw.elka.tinyrepo.client.common.DELETERequest;
import pl.edu.pw.elka.tinyrepo.client.common.GETRequest;
import pl.edu.pw.elka.tinyrepo.client.common.MKCOLRequest;
import pl.edu.pw.elka.tinyrepo.client.common.MOVERequest;
import pl.edu.pw.elka.tinyrepo.client.common.MockRequest;
import pl.edu.pw.elka.tinyrepo.client.common.PROPFINDRequest;
import pl.edu.pw.elka.tinyrepo.client.common.PUTRequest;

/**
 * 
 * @author Anna Stępień
 * @version 13-12-2011
 *
 */
class ControlPanel extends JPanel
{

	private static final long serialVersionUID = 1L;
	/** Kolejka blokująca Requestów */
	private final BlockingQueue<MockRequest> requestQueue;
	private final JButton getButton;
	private final JButton putButton;
	private final JButton mkcolButton;
	private final JButton copyButton;
	private final JButton moveButton;
	private final JButton deleteButton;
	private final JButton propfindButton;
	
	public ControlPanel(final BlockingQueue<MockRequest> requestQueue)
	{
		this.requestQueue = requestQueue;
		setPreferredSize(new Dimension(640, 40));
		setLayout(new FlowLayout());
		
		getButton = new JButton("GET");
		putButton = new JButton("PUT");
		mkcolButton = new JButton("MKCOL");
		copyButton = new JButton("COPY");
		moveButton = new JButton("MOVE");
		deleteButton = new JButton("DELETE");
		propfindButton = new JButton("PROPFIND");
		
		add(getButton);
		add(putButton);
		add(mkcolButton);
		add(copyButton);
		add(moveButton);	
		add(deleteButton);
		add(propfindButton);

		
		getButton.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{		
				JTextField pathInput = new JTextField();
				JTextField destinationPathInput = new JTextField("/");
				final JComponent[] inputs = new JComponent[] {
				                new JLabel("Path: "),
				                pathInput, 
				                new JLabel("Destination path: "),
				                destinationPathInput
				};
				JOptionPane.showMessageDialog(null, inputs, "GET", JOptionPane.PLAIN_MESSAGE);
				String path = pathInput.getText();
				String destinationPath = destinationPathInput.getText();
				if (!path.isEmpty())
				{
					//utwórz mock request na podstawie ścieżki
					//i wrzuć go do kolejki
					GETRequest request = new GETRequest(path, destinationPath);
					try 
					{
						requestQueue.put(request);
					}
					catch (InterruptedException ex) 
					{
						ex.printStackTrace();
					}
				}
			}
		});
		
		putButton.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				JFileChooser fileChooser = new JFileChooser();
	
				int status = fileChooser.showOpenDialog(null);
				
				//wybrano plik
				//można teraz wybrać ścieżkę 
				if (status == JFileChooser.APPROVE_OPTION)
				{
					File selectedFile = fileChooser.getSelectedFile();
					String filePath = selectedFile.getPath(); // FIXED: + selectedFile.getFile() 
					System.out.println("View: file path: "+filePath);
					JTextField pathInput = new JTextField();
					final JComponent[] inputs = new JComponent[] {
					                new JLabel("Destination path: "),
					                pathInput
					};
					JOptionPane.showMessageDialog(null, inputs, "PUT", JOptionPane.PLAIN_MESSAGE);
					
					String destinationPath = pathInput.getText();
					
					if (!destinationPath.isEmpty())
					{
						//utwórz mock request na podstawie ścieżki
						//i wrzuć go do kolejki
						PUTRequest request = new PUTRequest(destinationPath, filePath);
						try 
						{
							requestQueue.put(request);
						}
						catch (InterruptedException ex) 
						{
							ex.printStackTrace();
						}
					}
				}
				
			}
		});
		
		mkcolButton.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				JTextField pathInput = new JTextField();
				final JComponent[] inputs = new JComponent[] {
				                new JLabel("Collection name: "),
				                pathInput
				};
				JOptionPane.showMessageDialog(null, inputs, "MKCOL", JOptionPane.PLAIN_MESSAGE);
				String collectionName = pathInput.getText();
				
				if (!collectionName.isEmpty())
				{
					//utwórz mock request na podstawie ścieżki
					//i wrzuć go do kolejki
					MKCOLRequest request = new MKCOLRequest(collectionName);
					try 
					{
						requestQueue.put(request);
					}
					catch (InterruptedException ex) 
					{
						ex.printStackTrace();
					}
				}
				
			}
		});
		
		copyButton.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				JTextField sourcePathInput = new JTextField();
				JTextField destinationPathInput = new JTextField();
				final JComponent[] inputs = new JComponent[] {
				                new JLabel("Source: "),
				                sourcePathInput,
				                new JLabel("Destination: "),
				                destinationPathInput
				};
				JOptionPane.showMessageDialog(null, inputs, "COPY", JOptionPane.PLAIN_MESSAGE);
				String sourcePath = sourcePathInput.getText();
				String destinationPath = destinationPathInput.getText();
				
				if (!sourcePath.isEmpty() && !destinationPath.isEmpty())
				{
					//utwórz mock request na podstawie ścieżki
					//i wrzuć go do kolejki
					COPYRequest request = new COPYRequest(sourcePath, destinationPath);
					try 
					{
						requestQueue.put(request);
					}
					catch (InterruptedException ex) 
					{
						ex.printStackTrace();
					}
				}
				
			}
		});
		
		moveButton.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				JTextField sourcePathInput = new JTextField();
				JTextField destinationPathInput = new JTextField();
				final JComponent[] inputs = new JComponent[] {
				                new JLabel("Source: "),
				                sourcePathInput,
				                new JLabel("Destination: "),
				                destinationPathInput
				};
				JOptionPane.showMessageDialog(null, inputs, "MOVE", JOptionPane.PLAIN_MESSAGE);
				String sourcePath = sourcePathInput.getText();
				String destinationPath = destinationPathInput.getText();
				
				if (!sourcePath.isEmpty() && !destinationPath.isEmpty())
				{
					//utwórz mock request na podstawie ścieżki
					//i wrzuć go do kolejki
					MOVERequest request = new MOVERequest(sourcePath, destinationPath);
					try 
					{
						requestQueue.put(request);
					}
					catch (InterruptedException ex) 
					{
						ex.printStackTrace();
					}
				}
				
			}
		});
		
		deleteButton.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				JTextField pathInput = new JTextField();
				final JComponent[] inputs = new JComponent[] {
				                new JLabel("Path: "),
				                pathInput
				};
				JOptionPane.showMessageDialog(null, inputs, "DELETE", JOptionPane.PLAIN_MESSAGE);
				String path = pathInput.getText();
				
				if (!path.isEmpty())
				{
					//utwórz mock request na podstawie ścieżki
					//i wrzuć go do kolejki
					DELETERequest request = new DELETERequest(path);
					try 
					{
						requestQueue.put(request);
					}
					catch (InterruptedException ex) 
					{
						ex.printStackTrace();
					}
				}
				
			}
		});
		
		propfindButton.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				JTextField pathInput = new JTextField();
				final JComponent[] inputs = new JComponent[] {
				                new JLabel("Path: "),
				                pathInput
				};
				JOptionPane.showMessageDialog(null, inputs, "PROPFIND", JOptionPane.PLAIN_MESSAGE);
				String path = pathInput.getText();
				
				if (!path.isEmpty())
				{
					//utwórz mock request na podstawie ścieżki
					//i wrzuć go do kolejki
					PROPFINDRequest request = new PROPFINDRequest(path);
					try 
					{
						requestQueue.put(request);
					}
					catch (InterruptedException ex) 
					{
						ex.printStackTrace();
					}
				}				
			}
		});
	}
}
